<%

Dim RoleID
Dim RoleName

Sub Role_Add()	

	
	RoleName = reform("RoleName")
	MenuID = reform("MenuID")


	Call  DenyIfRecordExist("Admin_Role","RoleName","1",RoleName,"角色名称已经存在!")

	Call Check_Length(RoleName,50,1,"菜单名称")


	Conn.BeginTrans
	On Error Resume Next

	sql = "SET NOCOUNT ON;"
	sql = sql & "INSERT INTO Admin_Role(RoleName) VALUES( " &_
		  "'" & RoleName & "')" 
	sql = sql & ";SELECT @@IDENTITY"

	'writebr sql
    Set rs = conn.execute(sql)
	RoleID = rs(0)

	rs.close

	Call  Add_Role_Menu(RoleID,MenuID)


	if err.number =0 then  
	    conn.CommitTrans  
		AlertBox "角色增加成功！","Role_Query.asp"
	else 
		conn.RollbackTrans 
		AlertBox "角色增加失败！","Role_Query.asp"
	end if



End Sub

Sub Role_Edit()	

	RoleID   = reform("RoleID")
	RoleName = reform("RoleName")
	MenuID = reform("MenuID")

	Call Check_IsNumeric(RoleID)

	Call Check_Length(RoleName,50,1,"菜单名称")


	Conn.BeginTrans
	On Error Resume Next

	sql = "UPDATE Admin_Role Set " &_
		  " RoleName = '" & RoleName & "'" &_
		  " WHERE RoleID = "&RoleID


    conn.execute(sql)

	Call  Update_Role_Menu(RoleID,MenuID)

	if err.number =0 then  
	    conn.CommitTrans  
		AlertBox "角色修改成功！","Role_Query.asp"
	else 
		conn.RollbackTrans 
		AlertBox "角色修改失败！","Role_Query.asp"
	end if

End Sub

Sub Role_Del()


	RoleID = Reform("Op_ID")	

	'Writebr Op_ID
	'endd

	sql = "DELETE FROM  Admin_Role WHERE RoleID  IN  ("&RoleID &")"&_
		  ";DELETE  FROM  Admin_Role_User WHERE RoleID IN  ("&RoleID &")"&_
		  ";DELETE  FROM  Admin_Role_Menu WHERE RoleID IN  ("&RoleID &")"
	On Error Resume Next
		  
    conn.execute(sql)

	if err.number =0 then  
		AlertBox "角色增加成功！","Role_Query.asp"
	else 
		AlertBox "角色增加失败！","Role_Query.asp"
	end if
	
End Sub

Sub Get_Role_Info(RoleID)


	sql = "SELECT * FROM Admin_Role WHERE RoleID = " &RoleID

	Set rs = conn.execute(sql)
	
	If Not rs.eof Then

		RoleID   = rs("RoleID")
		RoleName = rs("RoleName")
		

	End If
	

End Sub 

'增加角色对应菜单表
Sub Add_Role_Menu(RoleID,MenuID)

	If MenuID="" Then Exit Sub 

	MenuID_arr = Split(MenuID,",")

	For i=0 To UBound(MenuID_arr)
		iMenuID = Trim(MenuID_arr(i))
		If iMenuID<>"" Then
			sql = "INSERT INTO Admin_Role_Menu(RoleID,MenuID) VALUES("&RoleID&","&iMenuID&")"
			conn.Execute(sql)
		End If

	Next
	
End Sub


'修改角色对应菜单表
Sub Update_Role_Menu(RoleID,MenuID)

	'先删除全部权限
	conn.Execute("DELETE FROM Admin_Role_Menu WHERE RoleID="&RoleID)	

	'未设置时退出
	If MenuID="" Then
		Exit Sub 
	End If

	'重置权限

	MenuID_arr = Split(MenuID,",")

	For i=0 To UBound(MenuID_arr)
		iMenuID = Trim(MenuID_arr(i))
		If iMenuID<>"" Then
			sql = "INSERT INTO Admin_Role_Menu(RoleID,MenuID) VALUES("&RoleID&","&iMenuID&")"
			conn.Execute(sql)
		End If

	Next
	
End Sub

Function Get_MenuIDArray(RoleID)

		Set rsTemp = Conn.execute ("SELECT MenuID FROM Admin_Role_Menu WHERE RoleID="&RoleID)

		If rsTemp.Eof Then
			Get_MenuIDArray = ""
		Else
			Get_MenuIDArray = rsTemp.GetRows
			
		End If
		
		'xx=rsTemp.GetRows
	
		Set rsTemp = Nothing 
End Function

Function Get_MenuIDChecked(MenuID,MenuIDArray)

	Dim IsCheck

	IsCheck=False

	If IsArray(MenuIDArray)  Then
		For i=0 to Ubound(MenuIDArray,2)
			If MenuID =  MenuIDArray(0,i)  Then 
				IsCheck = True 
				'writebr MenuIDArray(0,i)
			End If
			
		Next

		If IsCheck=True Then
			Get_MenuIDChecked = "Checked"
		Else
			Get_MenuIDChecked = ""
		End If
	Else
		Get_MenuIDChecked = ""
	End If

End Function


%>

